package mote;

import java.util.Date;
import java.lang.String;

/**
 * Mote is a class that describes a single mote
 * @author Khalid Alhokail, Vineet Gupta, Oren Melzer, David Schneider
 *
 */
public class Mote{
	private MoteInterface connection;
	private byte LEDs;
	private int[] lastReadings;
	private int oldestReadingNdx;
	static final int NUM_READINGS = 100;
	
	/**
	 * Sets the LEDs of the mote.
	 * @param LEDs		The LEDs of the mote as a byte. 
	 * <br>	1 for LED 0.
	 * <br>	2 for LED 1.
	 * <br> 4 for LED 2.
	 * <br>For example, 7 is all on, and 0 is all off. 
	 * @return	If the packet was successfully queued to be transfered to the
	 * 			GameHub to be relayed to the mote. Will fail if the mote does
	 * 			not exist, the serial write buffer is full, or there is no valid
	 * 			MoteInterface set.
	 */
	public boolean setLEDs(byte LEDs){
		if(connection != null){
			if(connection.setLEDs(id, LEDs)){
				this.LEDs = LEDs;
				return true;
			}
			return false;	
		}else
			return false;
	}
	
	/**
	 * Gets the expected value of the LEDs.
	 * @return	The expected value of the LEDs as a byte.
	 * <br>	1 for LED 0.
	 * <br>	2 for LED 1.
	 * <br> 4 for LED 2.
	 * <br>For example, 7 is all on, and 0 is all off. 
	 */
	public byte getLEDs(){
		return LEDs;
	}
	
	/**
	 * Creates a mote with id of id and no MoteInterface
	 * @param	id	The id of the new mote
	 */
	public Mote(int id){
		this.id=id;
		this.reading = 0;
		this.recievedTime = null;
		connection = null;
		LEDs = 0;
		lastReadings = new int[NUM_READINGS];
		for(int i = 0; i < NUM_READINGS; ++i)
		{
			lastReadings[i]=0;
		}
		oldestReadingNdx = 0;
	}
	
	/**
	 * Creates a mote with id of id and a MoteInterface
	 * @param	id	The id of the new mote
	 * @param	conn	the MoteInterface variable
	 */
	public Mote(int id, MoteInterface conn){
		this(id);
		connection = conn;
	}
	
	/**
	 * Sets a connection to a MoteInterface
	 * @param	conn	the MoteInterface variable
	 */
	protected void setConnection(MoteInterface conn){
		connection = conn;
	}
	
	/**
	 * Sets a light reading for the mote
	 * @param	reading	The new value of light reading
	 */
	protected void SetReading(int reading){
		recievedTime = new Date();
		this.reading = reading;
		lastReadings[oldestReadingNdx] = reading;
		oldestReadingNdx = (oldestReadingNdx + 1) % NUM_READINGS;
	}
	
	/**
	 * Gets the id of the mote
	 * @return the id of the mote
	 */
	public int GetId(){
		return id;
	}
		
	/**
	 * Gets the received time of last reading
	 * @return the recieved time of last reading
	 */
	public Date ReceivedTime(){
		return recievedTime;
	}
	
	/**
	 * Gets the last reading of the mote
	 * @return the last reading of the mote
	 */
	public int Reading(){
		return reading;
	}
	
	/**
	 * Gets the avarage of last count readings
	 * @param count	the number of readings to avarage
	 * @return the avarage of last count readings
	 */
	public double AverageReading(int count)
	{
		double sum = 0;
		int x;
		for(int i = 0; i < count; ++i)
		{
			x = (oldestReadingNdx - i - 1);
			if(x < 0) {
				x += NUM_READINGS;
			}
			sum += lastReadings[x];
		}
		return sum /(double)count;
	}
	
	public String toString(){
		return id+":"+reading+":"+recievedTime;
	}

	private int id;
	private int reading;
	private Date recievedTime;
	
} /* class Mote */
